home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / EMPTY.ASC < prev    next >
Text File  |  1997-06-20  |  2KB  |  79 lines

  1. _The Empty Member C++ Optimization_
  2. by Nathan Myers
  3.  
  4.  
  5. Listing One
  6. template <class T>
  7. class allocator {   // an empty class
  8.   static T* allocate(size_t n)
  9.     { return (T*) ::operator new(n * sizeof T); }
  10.   . . .
  11. };
  12.  
  13. Listing Two
  14. template <class T, class Alloc = allocator<T> >
  15. class list {
  16.   Alloc alloc_; 
  17.   struct Node { . . . };
  18.   Node* head_;      
  19.  public:
  20.   explicit list(Alloc const& a = Alloc())
  21.     : alloc_(a) { . . . }
  22.   . . .
  23. };
  24.  
  25.  
  26. Listing Three 
  27. struct Bar { };
  28. struct Foo {
  29.   struct Bar a[2];
  30.   struct Bar b;
  31. };
  32. Foo f;
  33.  
  34. Listing Four
  35. template <class T, class Alloc = allocator<T> >
  36. class list : private Alloc {
  37.   struct Node { . . . };
  38.   Node* head_;      
  39.  public:
  40.   explicit list(Alloc const& a = Alloc())
  41.     : Alloc(a) { . . . }
  42.   . . .
  43. };
  44.  
  45. Listing Five
  46. template <class T, class Alloc = allocator<T> >
  47. class list {
  48.   struct Node { . . . };
  49.   struct P : public Alloc {
  50.     P(Alloc const& a) : Alloc(a), p(0) { }
  51.     Node* p;
  52.   };
  53.   P head_;
  54.  public:
  55.   explicit list(Alloc const& a = Alloc())
  56.       : head_(a) { . . . }
  57.   . . .
  58. };
  59.  
  60. Listing Six
  61. template <class Base, class Member>
  62. struct BaseOpt : Base {
  63.   Member m;
  64.   BaseOpt(Base const& b, Member const& mem) 
  65.    : Base(b), m(mem) { }
  66. };
  67.  
  68. Listing Seven
  69. template <class T, class Alloc = allocator<T> >
  70. class list {
  71.   struct Node { . . . };
  72.   BaseOpt<Alloc,Node*> head_;
  73.  public:
  74.   explicit list(Alloc const& a = Alloc())
  75.     : head_(a,0) { . . . }
  76.   . . .
  77. };
  78.  
  79.